#include "connectioninterface.h"

ConnectionInterface::ConnectionInterface()
{
    _numConnections = _connections.size(); // Set _numConnections to size of connection vector
}

ConnectionInterface::~ConnectionInterface()
{
    qDeleteAll(_connections.begin(), _connections.end()); // Delete all objects pointed to in the vector
    _connections.clear(); // Clear the pointers in the vector
}

void ConnectionInterface::connectToServer(int connectionID)
{
    if(_connections.size() > connectionID)
    {
        if(!_connections[connectionID]->isConnected())
        {
            _connections[connectionID]->connectToServer();
        }
        else
        {
            QApplication::beep();
        }
    }
    else
    {
        QApplication::beep();
    }
}

void ConnectionInterface::changeParameters(QString name, QString host, int port, QString authName, QString password, QString filterString, int connectionID)
{
    if(_connections.size() > connectionID)
    {
        _connections[connectionID]->changeParameters(name,host,port,authName,password,filterString);
    }
    else
    {
        QApplication::beep();
    }
}

void ConnectionInterface::disconnectFromServer(int connectionID)
{
    if(_connections.size() > connectionID)
    {
        if(_connections[connectionID]->isConnected())
        {
            _connections[connectionID]->disconnectFromServer();
        }
        else
        {
            QApplication::beep();
        }
    }
    else
    {
        QApplication::beep();
    }
}

void ConnectionInterface::writeToSocket(QString aprsPacket)
{
    for(int i = 0; i < _connections.size(); i++)
    {
        if(_connections[i]->isWriteEnabled())
        {
            _connections[i]->writeToSocket(aprsPacket);
        }
    }
}

bool ConnectionInterface::isConnected(int connectionID)
{
    if(_connections.size() > connectionID)
    {
        if(_connections[connectionID]->isConnected())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}

bool ConnectionInterface::newConnection(QString name, QString host, int port, QString authName, QString password, QString filterString, bool writeEnabled)
{
    ServerConnection* connection = new ServerConnection(name,host,port,authName,password,filterString,writeEnabled); // Creates a new connection Object
    connect(connection,SIGNAL(newData(QString)),this,SLOT(getData(QString))); // connects the signal emitted when a new packet is received on this connection
    _connections.push_back(connection); // Add this new connection object to the _connections vector
    return true;
}

void ConnectionInterface::getData(QString data)
{
    qDebug() << data;
    emit newData(data); // New packet from a connection
}

QString ConnectionInterface::getName(int connectionID)
{
    return _connections[connectionID]->getName();
}

int ConnectionInterface::getPortNumber(int connectionID)
{
    return _connections[connectionID]->getPortNumber();
}

QString ConnectionInterface::getHostName(int connectionID)
{
    return _connections[connectionID]->getHostName();
}

QString ConnectionInterface::getPassword(int connectionID)
{
    return _connections[connectionID]->getPassword();
}

QString ConnectionInterface::getUserName(int connectionID)
{
    return _connections[connectionID]->getUserName();
}

QString ConnectionInterface::getFilterString(int connectionID)
{
    return _connections[connectionID]->getFilterString();
}

QStringList ConnectionInterface::getConnectionNames()
{
    QStringList connections;
    for(int i = 0; i < _connections.size(); i++)
    {
        connections.append(_connections[i]->getName());
    }
    return connections;
}

void ConnectionInterface::removeConnection(int connectionID)
{
    _connections.remove(connectionID);
//    QVector<ServerConnection*> tempConnections;

//    for(int i = 0; i < _connections.size(); i++)
//    {
//        if(i != connectionID)
//        {
//            tempConnections.push_back(_connections[i]);
//        }
//    }

//    _connections.clear(); // Clear the pointers in the vector

//    for(int i = 0; i < tempConnections.size(); i++)
//    {
//        _connections[i] = tempConnections[i];
//    }
//    tempConnections.clear(); // Clear the pointers in the vector
}

bool ConnectionInterface::isTransmitEnabled(int connectionID)
{
    return _connections[connectionID]->isWriteEnabled();
}
